// components/buttom/buttom.js
const behavior1 = require("../../behavior/behavior1")
var util = require("../../utils/pageChangeUtils")
var app = getApp()
Component({
  options: {
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tishu: 0,
    array: [],
    switchFlag: false,
    index: 0,
    lastPage: "",
    nextPage: ""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _setImages: function (e) {
      let cacheImage = wx.getStorageSync('image')
      this.setData({
        lastPage: cacheImage.testCss[2],
        nextPage: cacheImage.testCss[3]
      })
    },
    bindPickerChange: function (e) {
      this.setData({
        index: Number(e.detail.value)
      })
      var a = e.detail.value
      this.triggerEvent("tongXin", {
        a,
        "type": "bindPickerChange"
      })
    },
    lastQuestion: function () {
      var e = this.data.index
      if (e > 0) {
        this.setData({
          index: this.data.index - 1
        })
        var a = this.data.index
        this.triggerEvent("tongXin", {
          a,
          "type": "lastQuestion"
        })
      }
    },
    nextQuestion: function () {
      var e = this.data.index
      if (e < this.data.tishu - 1) {
        this.setData({
          index: this.data.index + 1
        })
        var a = this.data.index
        this.triggerEvent("tongXin", {
          a,
          "type": "nextQuestion"
        })
      }
    }
  },
  lifetimes: {
    attached() {
      this._setImages()
      var ButtomFlag = app.globalData.ButtomFlag
      var topSubject = app.globalData.selectLib
      if (ButtomFlag) {
        var subject = wx.getStorageSync('subject')
        //从数据库中查出考试题目和考试时间
        let testItem = wx.getStorageSync('testItem')
        util.findTestItem(topSubject,testItem).then(res=>{
          var len = res.items
          var arr = new Array(len)
          for (var i = 0; i < len; i++) {
            arr[i] = subject[i].question.substring(0, 22);
          }
          this.setData({
            array: arr,
            tishu: len,
          })
        })
      } else {
        var flag = app.globalData.practiceFlag
        if (flag) {
          //此时代表从开始完成后的结果页面跳转过来的
          var subject = app.globalData.practiceWrong
        }
        if (!flag) {
          var subject = wx.getStorageSync('subject')
        }
        var len = subject.length
        var arr = new Array(len)
        for (var i = 0; i < len; i++) {
          arr[i] = subject[i].question.substring(0, 22);
        }
        this.setData({
          tishu: len,
          array: arr,
        });
      }
    }
  }
})